//XML_Util.cpp
#include "XML_Util.h"
#include <cstdio>

//Constructor
XML_Util::XML_Util()
{
}

//Destructor
XML_Util::~XML_Util()
{
}

//GetXMLDeclaration
string XML_Util::GetXMLDeclaration( string version, string encoding )
{
	string buffer;
	buffer = "";

	sprintf( const_cast<char*>(buffer.c_str()) ,
			 "<?xml version=\"%s\" encoding=\"%s\" ?>",
			 const_cast<char*>( version.c_str() ),
			 const_cast<char*>( encoding.c_str() ) );

	return buffer;
}


//CreateElement
string XML_Util::CreateElement( string elementName, string attribute )
{
	string buffer;
	buffer = "";

	if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) != 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s %s>",
				 const_cast<char*>( elementName.c_str() ),
				 const_cast<char*>( attribute.c_str() ) );
	}
	else if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) == 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s>",
				 const_cast<char*>( elementName.c_str() ) );
	}

	return buffer;	
}


//CreateTag
string XML_Util::CreateTag( string tagName, string attribute, string tagContent )
{
	string buffer;
	buffer = "";

	if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) != 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s %s>%s</%s>",
				 const_cast<char*>( tagName.c_str() ),
				 const_cast<char*>( attribute.c_str() ),
				 const_cast<char*>( tagContent.c_str() ),
				 const_cast<char*>( tagName.c_str() ) );
	}
	else if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) == 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s>%s</%s>",
				 const_cast<char*>( tagName.c_str() ),
				 const_cast<char*>( tagContent.c_str() ),
				 const_cast<char*>( tagName.c_str() ) );
	}

	return buffer;
}



//CreateStartElement
string XML_Util::CreateStartElement( string elementName, string attribute )
{
	string buffer;
	buffer = "";

	if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) != 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s %s>",
				 const_cast<char*>( elementName.c_str() ),
				 const_cast<char*>( attribute.c_str() ) );
	}
	else if( strcmp( const_cast<char*>( attribute.c_str() ), "" ) == 0 )
	{
		sprintf( const_cast<char*>(buffer.c_str()),
				 "<%s>",
				 const_cast<char*>( elementName.c_str() ) );
	}

	return buffer;	
}



//CreateCDATASection
string XML_Util::CreateCDATASection( string contentsOfCDATA )
{
	string buffer;
	buffer = "";

	sprintf( const_cast<char*>(buffer.c_str()),
			 "<![CDATA[  %s  ]]>",
			 const_cast<char*>( contentsOfCDATA.c_str() ) );

	return buffer;
}



//CreateEndElement
string XML_Util::CreateEndElement( string elementName )
{
	string buffer;
	buffer = "";
	cout << "elementName = " << elementName << endl;
	
	sprintf( const_cast<char*>(buffer.c_str()),
			 "</%s>",
			 const_cast<char*>( elementName.c_str() ) );

	return buffer;
}
